<?php
/**
 * Medical Monitoring Grid.
 * This module will display the current medical monitoring of 
 * a medical center.
 * 
 * @author Xavier VILAIN
 * @package Nawa\MedicalStudy\MedicalCenter\Monitoring
 * @license #license-nawa#
 * 
 */

use Fox\Behaviors;
use Fox\Grid\FieldCat\Field\Renderer\Image;
use Nawa\Survey;
use Fox\Grid\FieldCat\Field\Renderer\Progress;
use Nawa\MedicalStudy\MedicalCenter\Monitoring;
use Nawa\Messages;
use Fox\Ddic\DataElement\Formater\Void;
use Fox\Grid\FieldCat\Field\Renderer\Token;
use Fox\Html\Button;
use Fox\Generator;
use Fox\Grid;
$xview = Xview::instance();
$msid = $xview->val('msid');
$mcid = $xview->val('mcid');
$keys = array('msid' =>$msid,'mcid'=>$mcid );
$grid = new Grid('gMonitorings');
$grid->fieldcat(array(
					array('monitoring_reference','reference'),
					array('monitoring_type','type'),
					'monitoring_date',
					'monitoring_partners',
					array('monitoring_progress','progress'),
					array('monitoring_status','status'),
					'ms_resume',
					'bupload'
					));
$grid->fieldcat()->switchToObjectMode();

// Load all monitoring status
$statusLabel = array();
$statuses = Fox\Dao::finder(Monitoring\Status::className())
				->where('language = ?',array($xview->language()))
					->find();
foreach($statuses as $status) {
	$statusLabel[$status->getId()] = $status->label();
}
// Load all monitorings surveys
$surveys = Fox\Dao::finder(Survey::className())
					->find();
foreach($surveys as $survey) {
	$types[$survey->getId()] = $survey->label();
}					
$grid->fieldcat()->setGenerators(array(
	'reference' => Generator::useFunction(function($data) {
		return $data->monitoringid;
	}),
	'type' => Generator::useFunction(function($data) use ($types) {
		return $types[$data->type];
	}),
	'monitoring_partners' => Generator::useFunction(function($data) {
		$value = $data->isValitated();
		foreach($data->partners() as $partner) {
			$detail[] = array('label' => \Nawa\Customer::fullName($partner->customerid),'customerid' => $partner->customerid,'removable' => !$value);
		}
		if(isset($detail)) {
			return $detail;
		}
		return null;
	}),
	'progress' => Generator::useFunction(function($data) {
		return $data->progress;
	}),
	'status' => Generator::useFunction(function($data) use($statusLabel) {
		return $statusLabel[$data->status];
	}),
	'bupload' => Generator::useValue('?'),
));
$grid->fieldcat()->field('ms_resume')->setGenerator(
	Generator::useFunction(function($data){
		$documentLink = $data->lastDocument();
		if(!empty($documentLink)) {
			return Generator::generateValue(Fox\Generator::url(
						'core',
						'files',
						'download',
						array('id' => $documentLink->docid,'xvtrs' => Generator::useValue('none'))),
						$data);
		}
		return null;
	})
);
$grid->fieldcat()->field('bupload')
	->label(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::IMPORT_SIGNED_REPORT))
	->renderers(new Image())
	->renderer()
		->behaviors(Behaviors::action( Generator::url('medicalstudies','monitorings','upload',
					array('inlineid' => Generator::call('getInLineId'),
										  'doctype' => Monitoring::DOC_TYPE,
											'caller' => 'MC_gMonitorings',
											)
					),
					true))
	;
	
$grid->fieldcat()->field('ms_resume')->label(Msg(Messages::BUNDLE,Messages::DOWNLOAD_PDF));

$grid->fieldcat()->field('monitoring_partners')
	->label(Msg(Messages::BUNDLE,Messages::INVESTIGATORS_PRESENT))
	->width(20)
	->renderers(new Token(Generator::url('medicalstudies','partners','unassign',array(
												'msid' =>Generator::useElement('msid'),
												'mcid' => Generator::useElement('mcid'),
												'monitoringid' => Generator::useElement('monitoringid'),
												'customerid' => Generator::useElement('customerid')))))
	->formater(new Void());
$grid->fieldcat()->field('progress')
	->renderers(new Progress())
	->formater(new Void());
// On Click Action
$grid->onClick(Generator::redirect(Generator::url('medicalstudies','medicalcenters','monitoring',
		array('msid' =>Generator::useAttribute('msid'),
			  'mcid' => Generator::useAttribute('mcid'),
			  'monitoringid' => Generator::useAttribute('monitoringid'))
)));
$grid->source(Generator::url('medicalstudies','medicalcenters','monitorings',$keys));
// Add a new Monitroing Button
$grid->footer()->add(new Button(array('create','createMonitoring'),
			Generator::url('medicalstudies','medicalcenters','monitoring',
					array('mode' => 'create','mcid' => $mcid,'msid' => $msid)))
			)
			->label(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::ADD_A_MONITORING));
if($xview->ajax()) {
	$ms = $xview->component('medicalstudies','object',$keys);
	$centerPartner = $ms->findMedicalCentersPartners($msid.':'.$mcid);
	// Load Monitorings Data
	$data = $centerPartner->monitorings();
	$grid->initialize($data,true);
	echo $grid->display();
} else {
	return $grid;
}